package com.example.a20112.ui_demo.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.example.a20112.ui_demo.FoodListActivity;
import com.example.a20112.ui_demo.R;
import com.example.a20112.ui_demo.beans.ShopBean;
import com.example.a20112.ui_demo.model.Constants;
import com.squareup.picasso.Picasso;

import java.util.List;

public class ShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List<ShopBean> mDataList;
    private LayoutInflater mLayoutInflater;
    public ShopAdapter(Context mContext,List mDataList){
        this.mContext=mContext;
        this.mDataList=mDataList;
        mLayoutInflater=LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_shop, viewGroup,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
      final ShopBean entity=(ShopBean)mDataList.get(position);
        if(null==entity)
           return;
       ViewHolder viewHolder=(ViewHolder)holder;
       viewHolder.res_name.setText(entity.getShopname());
       viewHolder.res_address.setText(entity.getAddress());
       viewHolder.res_bar.setRating(entity.getLevel());
       Picasso.get()
                .load(Constants.BASE_URL+entity.getPic())
                .into(viewHolder.res_image);

       viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent = new Intent(mContext,FoodListActivity.class);
               intent.putExtra("shop_id",entity.getShop_id());
               mContext.startActivity(intent);
           }
       });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView res_name,res_address;
        ImageView res_image;
        RatingBar res_bar;
        public ViewHolder(View view) {
            super(view);
            res_name=(TextView)view.findViewById(R.id.fo_name);
            res_address=(TextView)view.findViewById(R.id.fo_price);
            res_image=(ImageView)view.findViewById(R.id.fo_image);
            res_bar=(RatingBar)view.findViewById(R.id.res_bar);

        }
    }

}
